<template>
	<view class="wrap">
		<view class="top">
			<view class="item">
				<view class="left">收货人</view>
				<u-input type="text" v-model="address.name" placeholder-class="line" fontSize="17" clearable
					border="none" placeholder="请填写收货人姓名" />
				</u-input>
			</view>
			<view class="item">
				<view class="left">手机号码</view>
				<u-input type="number" v-model="address.phone" maxlength="11" border="none" clearable
					placeholder-class="line" placeholder="请填写收货人手机号" />
				</u-input>
			</view>
			<view class="item">
				<view class="left">所在地区</view>
				<view class="right" @click="lotusAddressData.visible = true">
					<view class="address" :style="{color:region ? 'black' : '#c0c4cc'}">
						{{region ? region : '点击选择省市县'}}
					</view>
					<view>
						<u-icon name="arrow-right" :size="22" color="#999999"></u-icon>
					</view>
				</view>
			</view>
			<view class="item address">
				<view class="left">详细地址</view>
				<textarea type="text" v-model="address.address" placeholder-class="line" placeholder="街道、楼牌等" />
			</view>
		</view>
		<view class="bottom">
			<view class="default">
				<view class="left">
					<view class="set">设置默认地址</view>
					<view class="tips">提醒：每次下单会默认推荐该地址</view>
				</view>
				<view class="right">
					<switch :checked="address.is_default == 1 ? true : false" color="red" @change="setDefault" />
				</view>
			</view>
		</view>
		<view class="addSite" @click="add" :style="[inputStyle]">
			<view class="add">
				<u-icon name="plus" color="#ffffff" class="icon" :size="30"></u-icon>提交
			</view>
		</view>
		<Winglau14-lotusAddress v-on:choseVal="choseValue" :lotusAddressData="lotusAddressData">
		</Winglau14-lotusAddress>
	</view>
</template>

<script>
	let access_token;
	export default {
		data() {
			return {
				lotusAddressData: {
					visible: false, //是否显示三级联动菜单
					provinceName: '', //省
					cityName: '', //市
					townName: '', //县
				},
				region: '', //最后返回的省市县的组合
				address: {
					name: '',
					phone: '',
					address: '',
					is_default: 0, //是否默认地址
				}
			};
		},
		computed: {
			inputStyle() {
				let style = {};
				if (uni.$u.test.mobile(this.address.phone) && this.address.name && this.address.address && this.region) {
					//style.color = "#fff";
					style.backgroundColor = uni.$u.color['warning'];
				}
				return style;
			}
		},
		async onLoad(options) {
			if (!uni.$u.utils.isLogin()) {
				return;
			}
		},
		methods: {
			async add(){
				if (uni.$u.test.mobile(this.address.phone) && this.address.name && this.address.address && this.region) {
					const params = {
						...this.address,
						province:this.lotusAddressData.provinceName,
						city:this.lotusAddressData.cityName,
						county:this.lotusAddressData.townName
					}
					access_token = uni.getStorageSync('access_token');
					let res = await uni.$u.http.post(`/api/address/`,params,{
						header: {
							Authorization: 'bearer ' + access_token
						}
					})
					if(res !== false){
						uni.$u.toast('地址添加成功');
						setTimeout(()=>{
							uni.$u.route({
								url:'/pages/cart/address',
								type:'redirect'
							})
						},1500)
					}					
				}
			},
			setDefault(e) {
				//console.log(e);
				this.address.is_default = e.detail.value ? 1 : 0;
			},
			choseValue(res) {
				console.log(res);
				this.lotusAddressData.visible = res.visible;
				if (res.isChose) {
					this.lotusAddressData.provinceName = res.province;
					this.lotusAddressData.cityName = res.city;
					this.lotusAddressData.townName = res.town;
					this.region = res.province + ' - ' + res.city + (res.town ? ' - ' + res.town : '')
				}
			}
		}
	};
</script>

<style lang="scss" scoped>
	/deep/ .line {
		color: $u-light-color;
		font-size: 17px;
	}

	.wrap {
		background-color: #f2f2f2;

		.top {
			background-color: #ffffff;
			border-top: solid 2rpx $u-border-color;
			padding: 22rpx;

			.item {
				display: flex;
				font-size: 32rpx;
				line-height: 100rpx;
				align-items: center;
				border-bottom: solid 2rpx $u-border-color;

				.left {
					width: 180rpx;
				}

				.right {
					display: flex;
					justify-content: space-between;
					align-items: center;
					width: 100%;
					flex: 1;
				}

				input {
					text-align: left;
				}
			}

			.address {
				padding: 20rpx 0;
				line-height: 1.5;

				textarea {
					// width: 100%;
					height: 150rpx;
					background-color: #f7f7f7;
					line-height: 60rpx;
					//margin: 40rpx auto;
					//padding: 20rpx;
				}
			}

			.site-clipboard {
				padding-right: 40rpx;

				textarea {
					// width: 100%;
					height: 150rpx;
					background-color: #f7f7f7;
					line-height: 60rpx;
					margin: 40rpx auto;
					padding: 20rpx;
				}

				.clipboard {
					display: flex;
					justify-content: center;
					align-items: center;
					font-size: 26rpx;
					color: $u-tips-color;
					height: 80rpx;

					.icon {
						margin-top: 6rpx;
						margin-left: 10rpx;
					}
				}
			}
		}

		.bottom {
			margin-top: 20rpx;
			padding: 40rpx;
			padding-right: 0;
			background-color: #ffffff;
			font-size: 28rpx;

			.tag {
				display: flex;

				.left {
					width: 160rpx;
				}

				.right {
					display: flex;
					flex-wrap: wrap;

					.tags {
						width: 140rpx;
						padding: 16rpx 8rpx;
						border: solid 2rpx $u-border-color;
						text-align: center;
						border-radius: 50rpx;
						margin: 0 10rpx 20rpx;
						display: flex;
						font-size: 28rpx;
						align-items: center;
						justify-content: center;
						color: $u-content-color;
						line-height: 1;
					}

					.plus {
						//padding: 10rpx 0;
					}
				}
			}

			.default {
				margin-top: 50rpx;
				display: flex;
				justify-content: space-between;
				border-bottom: solid 2rpx $u-border-color;
				line-height: 64rpx;

				.tips {
					font-size: 24rpx;
				}

				.right {}
			}
		}
	}

	.addSite {
		display: flex;
		justify-content: space-around;
		width: 600rpx;
		line-height: 100rpx;
		position: fixed;
		bottom: 30rpx;
		left: 80rpx;
		background-color: #aaa;
		border-radius: 60rpx;
		font-size: 18px;

		.add {
			display: flex;
			align-items: center;
			color: #fff;

			.icon {
				margin-right: 10rpx;
			}
		}
	}
</style>
